import { Entity, Column, ManyToOne, JoinColumn, OneToMany } from 'typeorm';
import { Event } from 'src/modules/event/entities/event.entity';
import { Group } from 'src/modules/group/entities/group.entity';
import {
  StagePlayerSourceTypeEnum,
  StageStatusEnum,
  StageTypeEnum,
} from 'src/common/constant/enums';
import { StageEvent } from 'src/modules/stage-event/entities/stage-event.entity';
import { Tournament } from 'src/modules/tournament/entities/tournament.entity';
import { Category } from 'src/modules/category/entities/category.entity';
import { GameRule } from 'src/modules/game-rule/entities/game-rule.entity';
import { RallyRule } from 'src/modules/rally-rule/entities/rally-rule.entity';
import { BaseEntity } from 'src/common/entity/BaseEntity';

@Entity('stage')
export class Stage extends BaseEntity {
  @Column()
  type: StageTypeEnum;

  @Column()
  name: string;

  @Column()
  status: StageStatusEnum;

  @Column()
  playerCount: number;

  @Column()
  playerType: number;

  @Column()
  teamMatchType: number;

  @ManyToOne(() => GameRule)
  @JoinColumn({ name: 'fk_gamerule_id' })
  gameRule: GameRule;

  @ManyToOne(() => RallyRule)
  @JoinColumn({ name: 'fk_rallyrule_id' })
  rallyRule: RallyRule;

  @Column({ name: 'fk_gamerule_id' })
  gameRuleId: number;

  @Column({ name: 'fk_rallyrule_id' })
  rallyRuleId: number;

  @Column()
  eliminationWinnerCount: number;

  // @Column()
  // eliminationSighType: string;

  @Column({ select: false })
  roundNameType: number;

  @Column()
  roundGroupCount: number;

  @Column()
  roundGroupWinnerCount: number;

  // @Column({ select: false })
  // roundLoopCount: number;

  // @Column()
  // roundOrganizationType: number;

  @Column()
  stageOrderInEvent: number;

  @Column({ name: 'fk_event_id' })
  eventId: number;

  @ManyToOne(() => Event, (event) => event.stages)
  @JoinColumn({ name: 'fk_event_id' })
  event: Partial<Event>;

  @OneToMany(() => Group, (group) => group.stage, {
    cascade: true,
  })
  groups: Partial<Group>[];

  @OneToMany(() => StageEvent, (stageEvent) => stageEvent.stage, {
    cascade: true,
  })
  stageEvents: StageEvent[];

  @Column({ name: 'fk_tournamentId' })
  tournamentId: number;

  @ManyToOne(() => Tournament)
  @JoinColumn({ name: 'fk_tournamentId' })
  tournament: Tournament;

  @Column({ name: 'fk_categoryId' })
  categoryId: number;

  @ManyToOne(() => Category)
  @JoinColumn({ name: 'fk_categoryId' })
  category: Category;

  @Column()
  mustFinishMaxCountGame: boolean;

  @Column()
  mustFinishMaxCountMatch: boolean;

  @Column()
  mustCheckIn: boolean; //是否需要签到

  @Column()
  playerSourceType: StagePlayerSourceTypeEnum;

  @Column({ name: 'fk_playerSourceStageId' })
  playerSourceStageId: number;

  @Column({ name: 'copyFromId', select: false })
  copyFromId: number;
}
